package com.situ.day3;

import java.util.Scanner;

import org.junit.Test;
import org.omg.CORBA.TRANSACTION_REQUIRED;

public class StudentManger {
	// main
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生数量：");
		int count = scanner.nextInt();
		Student[] students = new Student[count];
		//输入学生信息
		inputStudentsInfo(students);
		//打印学生信息
		printStudentsInfo(students);
		//根据条件搜索学生信息
		searchByCondition(students);
	}

	/**
	 * 根据条件搜索
	 * @param students
	 */
	private static void searchByCondition(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		// for (;;) {}
		while (true) {
			System.out.println("----------------");
			System.out.println("根据id查找：	1");
			System.out.println("根据名字查找：	2");
			System.out.println("根据年龄查找：	3");
			System.out.println("根据班级查找：	4");
			System.out.println("推出程序：		0");
			System.out.println("----------------");
			int type = scanner.nextInt();
			
			if (type == 0) {
				System.out.println("退出程序");
				break;
			}
			
			switch (type) {
			case 1://根据id查找
				System.out.println("id");
				System.out.println("请输入要查找的id：");
				int id = scanner.nextInt();
				searchById(students, id);
				break;
			case 2:
				System.out.println("name");
				System.out.println("请输入要查找的名字：");
				String name = scanner.next();
				searchByName(students, name);
				break;
			case 3:
				System.out.println("age");
				System.out.println("请输入要查找的年龄：");
				int age = scanner.nextInt();
				searchByAge(students, age);
				break;
			case 4:
				System.out.println("className");
				System.out.println("请输入要查找的班级：");
				String className = scanner.next();
				searchByClassName(students, className);
				break;
			default:
				break;
			}
		}
	}

	private static void searchByClassName(Student[] students, String className) {
		//Duplicate local variable isFind
		boolean isSearchByClassNameFind = false;
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			if (className.equals(student.getClassName())) {
				System.out.println(student);
				isSearchByClassNameFind = true;
			}
		}
		if (isSearchByClassNameFind == false) {
			System.out.println("没有该信息的学生");
		}
	}

	private static void searchByAge(Student[] students, int age) {
		boolean isSearchByAgeFind = false;
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			if (age == student.getAge()) {
				System.out.println(student);
				isSearchByAgeFind = true;
			}
		}
		if (isSearchByAgeFind == false) {
			System.out.println("没有该信息的学生");
		}
	}

	private static void searchByName(Student[] students, String name) {
		//Duplicate local variable isFind
		boolean isSearchByNameFind = false;
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			if (name.equals(student.getName())) {
				System.out.println(student);
				isSearchByNameFind = true;
			}
		}
		if (isSearchByNameFind == false) {
			System.out.println("没有该信息的学生");
		}
	}

	/**
	 * 根据id搜索存放学生类型数组里面有没有对应数据
	 * @param students
	 * @param id
	 */
	private static void searchById(Student[] students, int id) {
		boolean isSearchByIdFind = false;//初始认为根据id没有查找到
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			if (id == student.getId()) {
				System.out.println(student);
				isSearchByIdFind = true;
			}
		}
		/*if (!isSearchByIdFind) {
			System.out.println("没有该信息的学生");
		}*/
		if (isSearchByIdFind == false) {
			System.out.println("没有该信息的学生");
		}
	}

	/**
	 * 输入学生信息
	 * @param students
	 */
	private static void inputStudentsInfo(Student[] students) {
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < students.length; i++) {
			System.out.println("请输入学生id：");
			int id = scanner.nextInt();
			System.out.println("请输入学生姓名：");
			String name = scanner.next();
			System.out.println("请输入学生年龄：");
			int age = scanner.nextInt();
			System.out.println("请输入学生班级:");
			String className = scanner.next();
			Student student = new Student(id, name, age, className);
			students[i] = student;
		}
	}

	/**
	 * 打印 存放学生类型的数组
	 * @param students
	 */
	private static void printStudentsInfo(Student[] students) {
		for (int i = 0; i < students.length; i++) {
			Student student = students[i];
			System.out.println(student);
		}
	}
}
